package com.photography.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.photography.common.Result;
import com.photography.entity.Carousel;
import com.photography.exception.ErrorCode;
import com.photography.mapper.CarouselMapper;
import com.photography.service.CarouselService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;


import java.util.Date;
import java.util.List;

@Service
public class CarouselServiceImpl extends ServiceImpl<CarouselMapper, Carousel> implements CarouselService {

    /**
     * 新增轮播图
     *
     * @param carousel 轮播图信息
     * @return 结果
     */
    @Override
    public Result addCarousel(Carousel carousel) {
        // 验证必填字段
        if (!StringUtils.hasText(carousel.getImgUrl())) {
            return Result.error("图片路径不能为空");
        }
        if (!StringUtils.hasText(carousel.getCategory())) {
            return Result.error("分类不能为空");
        }


        // 设置创建和更新时间
        Date now = new Date();
        carousel.setCreateTime(now);
        carousel.setUpdateTime(now);

        boolean result = this.save(carousel);

        if (result) {
            return Result.success(carousel, "添加轮播图成功");
        } else {
            return Result.error("添加轮播图失败");
        }
    }

    /**
     * 删除产品
     *
     * @param id 轮播图ID
     * @return 结果
     */
    @Override
    public Result deleteCarousel(Integer id) {
        // 验证轮播图ID
        if (id == null) {
            return Result.error("轮播图ID不能为空");
        }

        // 检查轮播图是否存在
        Carousel existingCarousel = this.getById(id);
        if (existingCarousel == null) {
            return Result.error("轮播图不存在");
        }

        boolean result = this.removeById(id);
        if (result) {
            return Result.success(null, "删除轮播图成功");
        } else {
            return Result.error("删除轮播图失败");
        }
    }

    /**
     * 更新轮播图信息
     *
     * @param carousel 轮播图信息
     * @return 结果
     */
    @Override
    public Result updateCarousel(Carousel carousel) {
        if (carousel.getId() == null) {
            return Result.error("轮播图id不能为空", ErrorCode.PARAM_EMPTY.getCode());
        }

        Carousel findCarousel = this.getById(carousel.getId());
        if (findCarousel == null) {
            return Result.error("轮播图不存在");
        }

        carousel.setUpdateTime(new Date());

        boolean result = this.updateById(carousel);

        if (result) {
            return Result.success(carousel, "更新成功");
        } else {
            return Result.error("更新失败");
        }

    }

    /**
     * 分页查询轮播图列表
     */
    @Override
    public Page<Carousel> getCarouselPage(Page<Carousel> page, String category) {
        // 创建查询条件
        LambdaQueryWrapper<Carousel> queryWrapper = new LambdaQueryWrapper<>();

        // 添加筛选条件
        if (StringUtils.hasText(category)) {
            queryWrapper.eq(Carousel::getCategory, category);//标签（精准）
        }

        // 按注册时间降序排序
        queryWrapper.orderByDesc(Carousel::getCreateTime);

        // 执行分页查询
        return page(page, queryWrapper);
    }

    /**
     * 根据类别查询所有轮播图
     *
     * @param category 类别
     * @return 结果
     */
    @Override
    public Result getCarouselAll(String category) {
        // 添加日志输出，查看实际接收到的参数值
        System.out.println("接收到的category参数: [" + category + "]");
        
        // 去除参数前后的空格
        if (category != null) {
            category = category.trim();
        }
        
        if (category == null || category.isEmpty()) {
            return Result.error("类别不能为空", ErrorCode.PARAM_EMPTY.getCode());
        }
        
        // 创建查询条件
        LambdaQueryWrapper<Carousel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Carousel::getCategory, category);
        queryWrapper.orderByDesc(Carousel::getCreateTime);

        // 添加日志输出，查看实际执行的SQL条件
        System.out.println("查询条件: category = " + category);
        
        List<Carousel> carousels = this.list(queryWrapper);
        
        // 添加日志输出，查看查询结果数量
        System.out.println("查询结果数量: " + (carousels != null ? carousels.size() : 0));
        
        if (CollectionUtils.isEmpty(carousels)) {
            return Result.error("未查询到任何值", ErrorCode.DATA_NOT_EXIST.getCode());
        } else {
            return Result.success(carousels, "查询成功");
        }
    }
}
